package com.example.springbootcourse.service.impl;

import com.example.springbootcourse.mapper.NoticeMapper;
import com.example.springbootcourse.pojo.Notice;
import com.example.springbootcourse.pojo.NoticeExample;
import com.example.springbootcourse.service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class NoticeServiceImpl implements NoticeService {

    @Autowired
    NoticeMapper noticeMapper;

    @Override
    public List<Notice> queryNotice() {
        NoticeExample example = new NoticeExample();
        example.setOrderByClause("creatdate desc");

        return noticeMapper.selectByExample(example);
    }

    @Override
    public void saveNotice(Notice notice) {

        if (StringUtils.isEmpty(notice.getId())){
            notice.setId(String.valueOf(Math.random()).substring(2, 8));
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            notice.setCreatdate(sdf.format(new Date()));
            noticeMapper.insert(notice);
        }else {
            noticeMapper.updateByPrimaryKey(notice);
        }


    }

    @Override
    public void delNotice(String id) {
        noticeMapper.deleteByPrimaryKey(id);
    }


}
